package com.jrui.www.t7manager;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jrui.www.t7manager.constant.T7Set;
import com.jrui.www.t7manager.util.T7Util;

import java.util.List;

/**
 * Created by xdl on 2019/5/24.
 */

public class SettingCardAdapter extends ArrayAdapter<CardSettingBean> {
    private int resourceId;

    public SettingCardAdapter(Context context, int textViewResourceId,
                              List<CardSettingBean> CardSettings) {
        super(context, textViewResourceId, CardSettings);
        resourceId = textViewResourceId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CardSettingBean cardSettingBean = getItem(position); // 获取当前项的Fruit实例
        View view;
        ViewHolder viewHolder;
        if (convertView == null) {
//        这样传参就可以设置item的高度了
            view = LayoutInflater.from(getContext()).inflate(resourceId, null);
            viewHolder = new ViewHolder();
            viewHolder.settingName = (TextView) view.findViewById(R.id.setting_name);
            viewHolder.settingValue = (TextView) view.findViewById(R.id.setting_value);
            viewHolder.rightArrow = (ImageView) view.findViewById(R.id.right_arrow);
            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        //        获取配置的名称
        viewHolder.settingName.setText(cardSettingBean.getDisplayName());
//        获取UNIT
        String unit = cardSettingBean.getUnit();
        if (null == unit) {
            unit = "";
        } else {
            unit = new StringBuffer().append("/").append(unit).toString();
        }
//        获取配置的值 如果是枚举类型 那么 这个应该是循环查找出来的
        String value = cardSettingBean.getValue();

        if (null != value) {
            if (cardSettingBean.getType().equals(T7Set.SETTING_TYPE_ENUMERATION)) {
                value = T7Util.getDisplayNameByValue(value, cardSettingBean.getEnumEntry());
            }
            viewHolder.settingValue.setText(new StringBuffer().append(value).append(unit));
        } else {
            value = cardSettingBean.getDefaultValue();
            if (value == null) {
                value = "";
            } else {
                if (cardSettingBean.getType().equals(T7Set.SETTING_TYPE_ENUMERATION)) {
                    value = T7Util.getDisplayNameByValue(value, cardSettingBean.getEnumEntry());
                }
            }
            viewHolder.settingValue.setText(new StringBuffer().append(value).append(unit));
        }
//        对于非RO都需要出现弹出框来进行操作 这个时候箭头是需要显示的
        Boolean isHasDialog = null;
        isHasDialog = cardSettingBean.getAccessMode().equals(T7Set.ACCESS_MODE_RO) ? false : true;
        if (isHasDialog) {
            viewHolder.rightArrow.setVisibility(View.VISIBLE);
        } else {
            viewHolder.rightArrow.setVisibility(View.INVISIBLE);
        }
        return view;
    }



    class ViewHolder {
        TextView settingName;
        TextView settingValue;
        ImageView rightArrow;
    }
}
